import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.util.Enumeration;
import java.io.*;


public class ListContend extends List implements CommandListener{
	private FileManagerMIDlet filemanager;	
	private AudioPlayer audioplayer;
	private Display display;
	private String url, filename, filenamebefore, copyfileurl, copyfilename;
	private Enumeration file;
	private Command showroot,open,exit, back, create, delete, showcalculator, stop, showaudio, texteditor, read, copy, insert;
	private Image imagefolder, imageaudio, imagevideo, imageunknown;
	private FileConnection fileconn;
	private Connector conn;

	public ListContend(FileManagerMIDlet fm, Display dp){
		super("", Choice.IMPLICIT);
		this.filemanager = fm;			
		this.display = dp;
		open = new Command("Mo", Command.OK, 0);
		exit = new Command("Thoat", Command.EXIT, 1);
		showroot = new Command("Thu muc goc", Command.OK, 2);
		create = new Command("Tao moi", Command.OK,2);
		delete = new Command("Xoa", Command.OK, 2);
		back = new Command("Tro ve", Command.BACK,1);	
		showcalculator  = new Command("May tinh", Command.OK, 2);
		stop = new Command("Tat nhac", Command.OK, 2);
		showaudio = new Command("Hien thi bai hat", Command.OK, 2);
		texteditor = new Command("Soan van ban", Command.OK, 2);
		copy = new Command("Sao chep", Command.OK, 2);
		insert = new Command("Dan", Command.OK, 2);
		read = new Command("Read byte", Command.OK, 2);
		addCommand(back);
		addCommand(exit);	
		addCommand(create);	
		addCommand(copy);
		addCommand(delete);			
		addCommand(showroot);
		addCommand(showcalculator);		
		addCommand(texteditor);
		addCommand(read);
		addCommand(open);
		setSelectCommand(open);
		setCommandListener(this);
		try{
			imagefolder = Image.createImage("/folder.png");
			imageunknown = Image.createImage("/unknownfile.png");
			imageaudio = Image.createImage("/audio.png");
			imagevideo = Image.createImage("/video.png");
		}
		catch(Exception e){}
	}
	public void getContend(String newurl, String fileselected){		
		deleteAll();
		this.url = newurl;			
		try{			
			fileconn = (FileConnection) conn.open(url);	
			if(fileconn.isDirectory() == true){					
				file = fileconn.list("*.*",true);				
				while(file.hasMoreElements()){
					filename = (String) file.nextElement();				
					if(filename.endsWith("/"))
						append(filename, imagefolder);
					else if((filename.toUpperCase().endsWith(".MP3") == true) || (filename.toUpperCase().endsWith(".AAC") == true) || (filename.toUpperCase().endsWith(".MXMF") == true) || (filename.toUpperCase().endsWith(".MID") == true) || (filename.toUpperCase().endsWith(".AMR") == true))
						append(filename, imageaudio);			
					else if(filename.toUpperCase().endsWith(".3GP") == true)	
						append(filename, imagevideo);
					else
						append(filename, imageunknown);
				}						
			}
			else{				
			}			
		}		
		catch(Exception e){			
		}	

	}
	public String URL(){
		return url;
	}
	public String getUpitem(){
		if(url.endsWith("/") == true){
			filenamebefore = url.substring(0,url.length()-1);
			filenamebefore = filenamebefore.substring(filenamebefore.lastIndexOf('/')+1, filenamebefore.length());
			filenamebefore += "/";
		}
		else{
			filenamebefore = url.substring(filenamebefore.lastIndexOf('/')+1, url.length());
		}
		return filenamebefore;
	}
	public void setSelectedItem(String filename){
		int i = 0;
		while(this.getString(i).equals(filename) != true){			
			i ++;
			if(i > this.size())
				break;
		}
		this.setSelectedIndex(i, true);
	}
	public void addAudioCommand(AudioPlayer audio){
		addCommand(stop);
		addCommand(showaudio);
		this.audioplayer = audio;	
	}

	public void readByte(){
		int f = -1;
		String u = url + getString(getSelectedIndex());
		try{
		fileconn = (FileConnection) conn.open(u);
		InputStream is = fileconn.openInputStream();
		f = is.read();
		}
		catch(Exception e){}
		filemanager.showAlert(Integer.toString(f),this);
	}	
	
	public void Copy(){
		copyfileurl = url + getString(getSelectedIndex());
		copyfilename = getString(getSelectedIndex());
		addCommand(insert);		
	}
	
	public void Paste(){
		String pastefileurl = url + copyfilename;
		if(pastefileurl.equals(copyfileurl))
			filemanager.showAlert("Khong duoc dan o cung thu muc !", this);
		else{
			removeCommand(insert);
			try{
				fileconn = (FileConnection) conn.open(copyfileurl);
				FileConnection outfile = (FileConnection) conn.open(pastefileurl);
				outfile.create();
				InputStream is = fileconn.openInputStream();
				OutputStream os = outfile.openOutputStream();				
				for(long i = 0; i < fileconn.fileSize(); i ++)
					os.write(is.read());
				is.close();
				os.close();
				filemanager.showContend(url, null);
				filemanager.showAlert("Da copy xong !", this);
			}
			catch(Exception e){}
		}		
	}

	public void commandAction(Command c, Displayable d){
		if(c == open){
			filename = getString(getSelectedIndex());
			if(filename.endsWith("/") == true){				
				url += filename;
				filemanager.showContend(url, filename);
			}
			else if((filename.toUpperCase().endsWith(".MP3") == true) || (filename.toUpperCase().endsWith(".AAC") == true) || (filename.toUpperCase().endsWith(".MXMF") == true) || (filename.toUpperCase().endsWith(".MID") == true) || (filename.toUpperCase().endsWith(".AMR") == true)){
				if(audioplayer != null){
					audioplayer.Pause();
					filemanager.playAudio(url, filename);
				}
				else
					filemanager.playAudio(url, filename);
			}
			else if(filename.toUpperCase().endsWith(".3GP") == true){
				filemanager.playVideo(url, filename);
			}
			else
				filemanager.showAlert("Khong ho tro kieu file !", this);
		}
		if(c == back){
			getUpitem();
			url = url.substring(0,url.length()-1);
			url = url.substring(0, url.lastIndexOf('/')+1);
			if(url.equals("file:///"))
				filemanager.showRoot();
			else{			
				filemanager.showContend(url, filename);
				setSelectedItem(filenamebefore);
			}			
		}
		if(c == create){
			filemanager.showCreateForm();
		}
		if(c == delete){
			if(getSelectedIndex() == -1)
				filemanager.showAlert("Khong co gi de xoa !", this);
			else				
				filemanager.showDeleteForm(url, getString(getSelectedIndex()));			
		}
		if(c == exit){
			filemanager.exit();
		}
		if(c == showroot){			
			filemanager.showRoot();			
		}		
		if(c == showcalculator){
			filemanager.showCalculator(this);
		}
		if(c == stop){	
			audioplayer.Pause();
			removeCommand(stop);
			removeCommand(showaudio);
		}
		if(c == showaudio){
			removeCommand(stop);
			removeCommand(showaudio);
			display.setCurrent(audioplayer);
		}	
		if(c == texteditor){
			filemanager.showTextEditor();
		}	
		if(c == copy){
			Copy();
		}
		if(c == insert){
			Paste();
		}
		if(c == read){
			readByte();
		}								
	}
}